﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimplifyLauncher
{
    public class Shortcut
    {
        private string label, filePath, arguments, imagePath, copiedImageName;
        private int screenWidth, screenHeight;
        private Bitmap image;

        public Shortcut(string label, string filePath, string arguments, string imagePath)
        {
            this.label = label;
            this.filePath = filePath;
            this.arguments = arguments;
            this.imagePath = imagePath;

            screenWidth = Screen.PrimaryScreen.Bounds.Width;
            screenHeight = Screen.PrimaryScreen.Bounds.Height;
            SaveCopyOfImage();
        }

        public Bitmap GetImage()
        {
            return new Bitmap("../../Images/" + label + ".png");
        }

        private void Open()
        {
            Process myProcess = new Process();
            myProcess.StartInfo.FileName = filePath;
            myProcess.StartInfo.Arguments = arguments;
            //myProcess.StartInfo.FileName = "acroRd32.exe"; //not the full application path
            //myProcess.StartInfo.Arguments = "/A \"page=2=OpenActions\" C:\\example.pdf";
            myProcess.Start();
        }

        private void SaveCopyOfImage()
        {
            // public const string AllImages = "Image file|" + "*.png; *.jpg; *.jpeg; *.jfif; *.bmp;*.tif; *.tiff; *.gif";
            int width, height, ratio;

            using (var source = Image.FromFile(imagePath))
            {
                width = source.Width;
                height = source.Height;
                ratio = width / height;

                // If image width or height is larger than screen size, rescale to fit screen
                if (width > screenWidth)
                {
                    width = screenWidth;
                    height = width * ratio;
                }
                if (height > screenHeight)
                {
                    height = screenHeight;
                    width = height / ratio;
                }


                using (var copiedImage = new Bitmap(width, height))
                using (var graphics = Graphics.FromImage(copiedImage))
                {
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.DrawImage(source, new Rectangle(0, 0, width, height));
                    copiedImage.Save("../../Images/" + label + ".png");
                }
            }
        }

        public void Resize(string imageFile, string outputFile, double scaleFactor)
        {
            // public const string AllImages = "Image file|" + "*.png; *.jpg; *.jpeg; *.jfif; *.bmp;*.tif; *.tiff; *.gif";

            using (var srcImage = Image.FromFile(imageFile))
            {
                var newWidth = (int)(srcImage.Width * scaleFactor);
                var newHeight = (int)(srcImage.Height * scaleFactor);
                using (var newImage = new Bitmap(newWidth, newHeight))
                using (var graphics = Graphics.FromImage(newImage))
                {
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
                    newImage.Save(outputFile);
                }
            }
        }
    }
}
